﻿namespace YnabCsvConverter.Core.Converters
{
   using System.Collections.Generic;
   using System.Globalization;
   using System.Linq;

   /// <summary>
   /// Easybank Converter
   /// </summary>
   [ConverterName("Easybank")]
   public class EasybankConverter : BaseConverter, IConverter
   {
      /// <summary>
      /// Initializes a new instance of the <see cref="EasybankConverter" /> class.
      /// </summary>
      public EasybankConverter()
      {
         // The source cvs uses a 'german' culture formatted file.
         SourceCulture = CultureInfo.GetCultureInfo("de");
      }

      /// <summary>
      /// Converts this instance.
      /// </summary>
      /// <param name="sourceFilename">The source filename.</param>
      /// <param name="targetFilename">The target filename.</param>
      /// <param name="mode">The creation mode.</param>
      /// <returns>
      /// The Converter Result.
      /// </returns>
      public ConverterResult Convert(string sourceFilename, string targetFilename, CreationMode mode)
      {
         // Read specific records from source file.
         IEnumerable<EasybankRecord> easybankRecords = ReadRecords<EasybankRecord>(sourceFilename, ';', false);

         // Convert records from source file to YNAB records.
         IEnumerable<YnabRecord> ynabRecords = ConvertRecords(easybankRecords);

         // Write YNAB records to target file.
         WriteRecords(ynabRecords, targetFilename, mode);

         // Return a result
         return new ConverterResult {IsSuccess = true};
      }

      /// <summary>
      /// Converts to ynab lines.
      /// </summary>
      /// <param name="easybankRecords">The easybank records.</param>
      /// <returns></returns>
      /// <exception cref="System.NotImplementedException"></exception>
      public IEnumerable<YnabRecord> ConvertRecords(IEnumerable<EasybankRecord> easybankRecords)
      {
         return easybankRecords.Select(ConvertRecord).ToList();
      }

      /// <summary>
      /// Converts to ynab CSV line.
      /// </summary>
      /// <param name="easybankRecord">The easybank CSV line.</param>
      /// <returns></returns>
      /// <exception cref="System.NotImplementedException"></exception>
      public YnabRecord ConvertRecord(EasybankRecord easybankRecord)
      {
         var ynabRecord = new YnabRecord();

         ynabRecord.SetDate(easybankRecord.DateTime1, SourceCulture);
         ynabRecord.SetInflowOutflow(easybankRecord.Amount, SourceCulture);
         
         int index = easybankRecord.Text.IndexOf('|');

         if (index > 0)
         {
            // Found a | in thet text so the 
            // part before | shall be used as Payee
            // part after | shall be used as Memo
            ynabRecord.Payee = easybankRecord.Text.Substring(0,index);
            ynabRecord.Memo = easybankRecord.Text.Substring(index+1);
         }
         else
         {
            ynabRecord.Payee = easybankRecord.Text;
            ynabRecord.Memo = string.Empty;
         }
         
         ynabRecord.Category = string.Empty;
         
         return ynabRecord;
      }
   }
}
